package cn.bluemobi.dylan.step.ui.my.activity;

import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.base.BaseMvpActivity;
import cn.bluemobi.dylan.step.moder.bean.Goods;
import cn.bluemobi.dylan.step.moder.bean.MemberAccount;
import cn.bluemobi.dylan.step.moder.bean.Order;
import cn.bluemobi.dylan.step.moder.bean.User;
import cn.bluemobi.dylan.step.presenter.contract.my.IntegralContract;
import cn.bluemobi.dylan.step.presenter.contract.my.MyOrderContract;
import cn.bluemobi.dylan.step.presenter.my.activity.IntegralPresenter;
import cn.bluemobi.dylan.step.presenter.my.activity.MyOrderPresenter;
import cn.bluemobi.dylan.step.ui.my.adapter.GoodsAdapter;
import cn.bluemobi.dylan.step.ui.my.adapter.MyOrderAdapter;

/**
 * 我的订单
 */
public class MyOrderActivity extends BaseMvpActivity<MyOrderPresenter> implements MyOrderContract.View {

    @BindView(R.id.tool_bar)
    Toolbar mToolbar;
    @BindView(R.id.toolbar_back)
    ImageView toolbar_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.collapsingToolbarLayout)
    CollapsingToolbarLayout collapsingToolbarLayout;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_stepcount)
    TextView tv_stepcount;

    private List<Order> orderList;
    private MyOrderAdapter adapter;  //商品
    private MemberAccount memberAccount; //积分
    private User user;

    @Override
    protected int getLayout() {
        return R.layout.my_integral;
    }

    @Override
    protected void initView() {
        super.initView();
        initToolbar();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        memberAccount = (MemberAccount) getIntent().getSerializableExtra("MemberAccount");
        if(memberAccount!=null){
            tv_stepcount.setText(memberAccount.accountScore+"");
        }
//        mPresenter.preUser();
        initRefreshLoadMore();

    }

    private void initToolbar() {
        setSupportActionBar(mToolbar);
        tv_title.setText("我的订单");
        toolbar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initRefreshLoadMore(){
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                if(user!=null){
                    mPresenter.getMemberAccount(user.memberId);
                    mPresenter.httpGoodsOrderList(user.memberId,true);
                }else {
                    mPresenter.preUser();
                }
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                if(user!=null){
                    mPresenter.httpGoodsOrderList(user.memberId,false);
                }
            }
        });
        refreshLayout.autoRefresh();
    }
    private void setAdapterData() {
        if (orderList != null && orderList.size() > 0) {
            if (adapter == null) {
                adapter = new MyOrderAdapter(this, orderList);
                recyclerView.setAdapter(adapter);
            } else {
                adapter.notifyChanged(orderList);
            }
            adapter.setOnItemOnClilcklisten(new OnItemOnClilcklisten());
        }
    }


    @Override
    public void showUserInfo(User user) {
        if(user!=null){
            this.user = user;
            mPresenter.getMemberAccount(user.memberId);
            mPresenter.httpGoodsOrderList(user.memberId,true);
        }
    }

    @Override
    public void showMemberAccount(MemberAccount memberAccount) {  //积分
        if(memberAccount!=null){
            tv_stepcount.setText(memberAccount.accountScore+"");
        }
    }

    @Override
    public void httpGoods(List<Goods> result) {

    }

    @Override
    public void httpOrderSuceess(List<Order> result) {
        this.orderList = result;
        setAdapterData();
    }


    private class OnItemOnClilcklisten implements MyOrderAdapter.OnItemOnClicklisten {


        @Override
        public void onItemClick(View v, int position) {

        }

        @Override
        public void onBuyGoodsClick(View v, int position) {

        }

    }



    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }


    @Override
    public void showError(String msg) {

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void closeDialog() {
        if(this.isFinishing())return;
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

}
